import line
import gradient
import combine
import os
import dev.detector

output_path = './output/'
temp_path = './temp/'
input_path = './input/'

def get_img_list():
    
    name_list = os.listdir(input_path)
    img_list = list()

    for name in name_list:
        if name.endswith(".jpg"):
            save_name = input_path + str(name)
            img_list.append(save_name)
        if name.endswith(".png"):
            old_name = input_path + str(name)
            new_name = input_path + str(name).replace('.png', '.jpg')
            os.rename(old_name, new_name)
            img_list.append(new_name)

    return img_list

def del_tempfile():

    temp_list = os.listdir(temp_path)

    for name in temp_list:
        path = temp_path + str(name)
        os.remove(path)

def check_dir(path):
    if not os.path.isdir(path):
        os.mkdir(path)

def generator(img_name):

    check_dir(temp_path)
    check_dir(output_path)

    line.get_line(img_name, input_path, temp_path)
    gradient.generate_gradient(img_name, input_path, temp_path)
    combine.get_onelastkiss(img_name, input_path, temp_path, output_path)
    combine.get_half_onelast(img_name, input_path, temp_path, output_path)
    del_tempfile()

def head_generator(img_name):
    dev.detector.detect(img_name)

def init():

    img_list = get_img_list()
    if (len(img_list) == 0):
        print("No input picture detected")
    else:
        for img_name in img_list:
            head_generator(img_name)

    img_list = get_img_list()
    if (len(img_list) == 0):
        print("No input picture detected")
    else:
        for img_name in img_list:
            generator(img_name)
            

init()
